// main.c

#include "mplayer_player.h"
#include "bmp.h"
#include "gui.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>

#include "touch.h"
#include "font_display.h"

//

static pid_t child_pid_play_video = -1;

// 音频路径
const char *videoPath = "./assert/video/ad.mov";

/**
 * SIGINT 信号处理函数：
 * 将 SIGTERM 转发给子进程组，以确保 mplayer 被干净地终止
 */
static void sigint_handler(int signo)
{
    if (child_pid_play_video > 0)
    {
        killpg(child_pid_play_video, SIGTERM);
    }
    // 恢复默认 SIGINT 处理并重新触发，以终止父进程
    signal(SIGINT, SIG_DFL);
    raise(SIGINT);
}

int main(int argc, char *argv[])
{
    if(golden_finger_init()<0){
        printf("指纹识别初始化失败");
    }
    initGoodArea();
    init_goods_info();
    font_display_init();
    touch_init("/dev/input/event0");
    // 注册 SIGINT（Ctrl+C）处理器
    struct sigaction sa = {0};
    sa.sa_handler = sigint_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGINT, &sa, NULL);

    // 启动视频播放，使用 gui.h 中定义的 adArea
    child_pid_play_video = play_video(videoPath,
                                      adArea.x, adArea.y,
                                      adArea.width, adArea.height);
    if (child_pid_play_video < 0)
    {
        // 当启动播放器失败时输出英文错误信息
        fprintf(stderr, "Error: failed to start video player\n");
        return EXIT_FAILURE;
    }

    if (bmp_init() < 0)
    {
        fprintf(stderr, "Error: failed to initialize framebuffer\n");
        return EXIT_FAILURE;
    }

    fill_color(BG_COLOR, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    pay_back_click_event();

    // for (int i = 0; i < GOOD_NUM; i++)
    // {

    //     draw_bitmap(goodsInfo[i].picturePath, goodArea[i].x, goodArea[i].y, goodArea[i].width, goodArea[i].height);
    //     char text[264]; 
    //     sprintf(text, "%s %.1f", goodsInfo[i].name, goodsInfo[i].price);
    //     if(i!=6){
    //         font_display_text(goodArea[i].x, goodArea[i].y + GOOD_HEIGHT + 5, text, 30, getColor(0, 0, 0, 0));
    //     }else{//外星人特判
    //         font_display_text(goodArea[i].x-15, goodArea[i].y + GOOD_HEIGHT + 5, text, 30, getColor(0, 0, 0, 0));
    //     }
        
    // }

    // draw_dashed_border(goodArea[cursorGoodIndex].x - DASH_MARGIN, goodArea[cursorGoodIndex].y - DASH_MARGIN, GOOD_WIDTH + 2 * DASH_MARGIN, GOOD_HEIGHT + 2 * DASH_MARGIN, DASH_COLOR, DASH_WIDTH, DASH_LEN, GAP_LEN);

    // draw_dashed_border(goodArea[0].x-5, goodArea[0].y-5, GOOD_WIDTH+10, GOOD_HEIGHT+10, 0x67E6FF, 5, 5, 5);
    // draw_border(goodArea[1].x-5, goodArea[1].y-5, GOOD_WIDTH+10, GOOD_HEIGHT+10, 0x67E6FF,5);

    // 等待子进程（mplayer）退出
    int status;
    if (waitpid(child_pid_play_video, &status, 0) < 0)
    {
        perror("waitpid");
        return EXIT_FAILURE;
    }

    // 根据退出方式打印英文信息
    if (WIFEXITED(status))
    {
        printf("Player exited normally, exit code %d\n", WEXITSTATUS(status));
    }
    else if (WIFSIGNALED(status))
    {
        printf("Player was terminated by signal %d\n", WTERMSIG(status));
    }

    touch_stop();

    // 关闭字体，关闭画板
    font_display_close();
    return EXIT_SUCCESS;
}
